﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Windows.Threading;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for cautionWindow.xaml
    /// </summary>
    public partial class cautionWindow : Window
    {
        public static SqlConnection Con = homeWindow.Con;
        public static SqlDataAdapter da;

        DispatcherTimer dispatcherTimer = new DispatcherTimer();

        public cautionWindow()
        {
            InitializeComponent();
        }

        public void FillData()
        {
            try
            {
                Con.Open();
                da = new SqlDataAdapter(@"SELECT DISTINCT TOP (100) PERCENT ID_Reklame, 'Rp ' + Convert(varchar(20),dbo.format_currency(Pajak),1) as Pajak, Convert(varchar(12),Tanggal_Jatuh_Tempo_Pajak,103) as [Tanggal]
                                        FROM Reklame WHERE (DATEDIFF(day, Tanggal_Jatuh_Tempo_Pajak, GETDATE()) <= 0) AND (DATEDIFF(day, Tanggal_Jatuh_Tempo_Pajak, GETDATE()) >= -" + homeWindow.top.time + ")", Con);
                DataSet ds = new DataSet();
                da.Fill(ds, "Daftar Reklame Jatuh Tempo");
                cautionGrid.ItemsSource = ds.Tables["Daftar Reklame Jatuh Tempo"].DefaultView;
                if (cautionGrid.Columns.Count > 0)
                {
                    cautionGrid.Columns[2].Width = 150;
                }
                da = new SqlDataAdapter(@"SELECT DISTINCT TOP (100) PERCENT a.ID_Penyewaan, Pembayaran_ke, 'Rp ' + Convert(varchar(20),dbo.format_currency(Jumlah),1) as Jumlah, Convert(varchar(12),Tanggal,103) as [Tanggal]
                                        FROM RencanaPembayaran as a inner join menyewa as b on a.ID_Penyewaan = b.ID_Penyewaan WHERE (DATEDIFF(day, Tanggal, GETDATE()) <= 0) AND (DATEDIFF(day, Tanggal, GETDATE()) >= -" + homeWindow.top.time + ")", Con);
                DataSet dss = new DataSet();
                da.Fill(dss, "Daftar Rencana");
                dataGrid1.ItemsSource = dss.Tables["Daftar Rencana"].DefaultView;
                if (dataGrid1.Columns.Count > 0)
                {
                    dataGrid1.Columns[3].Width = 150;
                    dataGrid1.Columns[1].Width = 45;
                    dataGrid1.Columns[0].Width = 75;
                }
                Con.Close();

                for (int i = 7; i <= 30; i++)
                {
                    comboBox1.Items.Add(i);
                }
                comboBox1.SelectedItem = (object)homeWindow.top.time;
                if (cautionGrid.Columns.Count > 0)
                {
                    cautionGrid.Columns[1].CellStyle = (Style)Resources["RightAlignedCellStyle"];
                    cautionGrid.Columns[2].CellStyle = (Style)Resources["CenterAlignedCellStyle"];
                    cautionGrid.Columns[2].Width = 150;
                }
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (this.IsVisible == false)
            {
            }
            else
            {
                try
                {
                    Con.Open();

                    da = new SqlDataAdapter(@"SELECT DISTINCT TOP (100) PERCENT ID_Reklame, 'Rp ' + Convert(varchar(20),dbo.format_currency(Pajak),1) as Pajak, Convert(varchar(12),Tanggal_Jatuh_Tempo_Pajak,103) as [Tanggal]
                                            FROM Reklame WHERE (DATEDIFF(day, Tanggal_Jatuh_Tempo_Pajak, GETDATE()) <= 0) AND (DATEDIFF(day, Tanggal_Jatuh_Tempo_Pajak, GETDATE()) >= -" + homeWindow.top.time + ")", Con);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Daftar Reklame Jatuh Tempo");
                    cautionGrid.ItemsSource = ds.Tables["Daftar Reklame Jatuh Tempo"].DefaultView;
                    da = new SqlDataAdapter(@"SELECT DISTINCT TOP (100) PERCENT ID_Penyewaan, Pembayaran_ke, 'Rp ' + Convert(varchar(20),dbo.format_currency(Jumlah),1) as Jumlah, Convert(varchar(12),Tanggal,103) as [Tanggal]
                                            FROM RencanaPembayaran WHERE (DATEDIFF(day, Tanggal, GETDATE()) <= 0) AND (DATEDIFF(day, Tanggal, GETDATE()) >= -" + homeWindow.top.time + ")", Con);
                    DataSet dss = new DataSet();
                    da.Fill(dss, "Daftar Rencana");
                    dataGrid1.ItemsSource = dss.Tables["Daftar Rencana"].DefaultView;
                    if (cautionGrid.Columns.Count > 0)
                    {
                        cautionGrid.Columns[1].CellStyle = (Style)Resources["RightAlignedCellStyle"];
                        cautionGrid.Columns[2].CellStyle = (Style)Resources["CenterAlignedCellStyle"];
                        cautionGrid.Columns[2].Width = 150;
                    }
                    if (dataGrid1.Columns.Count > 0)
                    {
                        dataGrid1.Columns[1].CellStyle = (Style)Resources["CenterAlignedCellStyle"];
                        dataGrid1.Columns[2].CellStyle = (Style)Resources["RightAlignedCellStyle"];
                        dataGrid1.Columns[3].CellStyle = (Style)Resources["CenterAlignedCellStyle"];
                        dataGrid1.Columns[3].Width = 150;
                        dataGrid1.Columns[1].Width = 45;
                        dataGrid1.Columns[0].Width = 75;
                    }
                    Con.Close();
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
                }
            }
        }

        void cautionWindow_Closing(object sender, CancelEventArgs e)
        {
            //dispatcherTimer.Stop();
            if (!homeWindow.top.close)
            {
                Visibility = System.Windows.Visibility.Hidden;
                ((CancelEventArgs)e).Cancel = true;
            }
            else
            {
                Console.WriteLine("CAUTION CLOSING");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FillData();
            dispatcherTimer.Start();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            homeWindow.top.time = Int32.Parse(comboBox1.SelectedItem.ToString());
        }

        private void Window_Closed(object sender, EventArgs e)
        {

        }
    }
}
